package com.coocaa.ops.admin.v1.panel.provider.impl;

import com.coocaa.ops.admin.base.convert.CornerPositionConverter;
import com.coocaa.ops.admin.v1.dmp.biz.impl.DmpRegistryRlsInfoBizImpl;
import com.coocaa.ops.admin.v1.dmp.util.DmpRegistryRlsUtil;
import com.coocaa.ops.admin.v1.panel.biz.impl.*;
import com.coocaa.ops.admin.v1.panel.convert.GroupPanelSaveConverter;
import com.coocaa.ops.admin.v1.panel.convert.NormalPanelSaveConverter;
import com.coocaa.ops.admin.v1.panel.convert.PanelCommonSaveConverter;
import com.coocaa.ops.admin.v1.panel.provider.IPanelInfoRlsUpdateProvider;
import com.coocaa.ops.admin.v1.plugin.provider.IPluginUpdateProvider;
import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.panel.constant.BlockTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.SubPanelVersionConstants;
import com.coocaa.ops.common.core.biz.panel.entity.*;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description 板块信息原始表 关联信息更新提供者接口实现
 * @date : 2020-06-15 14:40:18
 */
@Service
public class PanelInfoRlsUpdateProviderImpl implements IPanelInfoRlsUpdateProvider {
    @Autowired
    private DmpRegistryRlsInfoBizImpl dmpRegistryRlsInfoBiz;
    @Autowired
    private PanelBlockRlsBizImpl panelBlockRlsBiz;
    @Autowired
    private PanelSubPanelRlsBizImpl panelSubPanelRlsBiz;
    @Autowired
    private SubPanelBizImpl subPanelBiz;
    @Autowired
    private BlockContentInfoBizImpl blockContentInfoBiz;
    @Autowired
    private GlobalPictureBizImpl globalPictureBiz;
    @Autowired
    private IPluginUpdateProvider pluginUpdateProvider;
    @Autowired
    private BlockExtendInfoBizImpl blockExtendInfoBiz;
    @Autowired
    private BlockPictureRlsBizImpl blockPictureRlsBiz;
    @Autowired
    private BlockExtendPosterInfoBizImpl blockExtendPosterInfoBiz;


    @Override
    public void saveRls(PanelInfoEntity inputEntity, Integer userId) {
        if (PanelTypeConstants.isNormal(inputEntity.getPanelType())) {
            this.handleSaveNormalPanel(inputEntity, userId);
        } else if (PanelTypeConstants.isGroup(inputEntity.getPanelType())) {
            this.handleSaveGroupPanel(inputEntity, userId);
        } else if (PanelTypeConstants.isPlugin(inputEntity.getPanelType())) {
            this.handleSavePluginPanel(inputEntity, userId);
        }
    }

    @Override
    public void deleteAllRls(Integer id, Integer version) {
        List<Integer> subPanelIdList = panelSubPanelRlsBiz.querySubPanelIdList(id, version);
        if (CollectionUtils.isNotEmpty(subPanelIdList)) {
            List<Integer> blockIdList = panelBlockRlsBiz.queryBlockIdListForGroupPanel(subPanelIdList);
            blockContentInfoBiz.deleteBatch(blockIdList);
            panelSubPanelRlsBiz.deleteRls(id, version);
            DmpRegistryRlsUtil.deleteDmpRls(subPanelIdList, ResourceTypeEnum.SUB_PANEL.getValue(), SubPanelVersionConstants.SUB_PANEL_VERSION);
        }
        List<Integer> blockIdList = panelBlockRlsBiz.queryBlockIdList(id, version);
        if (CollectionUtils.isNotEmpty(blockIdList)) {
            blockContentInfoBiz.deleteBatch(blockIdList);
            panelBlockRlsBiz.deleteRls(id, version);
            DmpRegistryRlsUtil.deleteDmpRls(id, ResourceTypeEnum.PANEL.getValue(), version);

        }

    }

    /**
     * 逻辑删除关联信息
     *
     * @param entity
     * @param id
     * @param version
     */
    @Override
    public void handleLogicDelRls(PanelInfoEntity entity, Integer id, Integer version) {
        if (PanelTypeConstants.isNormal(entity.getPanelType())) {
            List<Integer> blockIdList = panelBlockRlsBiz.queryBlockIdList(id, version);
            blockContentInfoBiz.logicDelBatch(blockIdList);
            panelBlockRlsBiz.logicDelRls(id, version);
            DmpRegistryRlsUtil.deleteDmpRls(id, ResourceTypeEnum.PANEL.getValue(), version);
        } else if (PanelTypeConstants.isGroup(entity.getPanelType())) {
            List<Integer> subPanelIdList = panelSubPanelRlsBiz.querySubPanelIdList(id, version);
            List<Integer> blockIdList = panelBlockRlsBiz.queryBlockIdListForGroupPanel(subPanelIdList);
            blockContentInfoBiz.logicDelBatch(blockIdList);
            panelSubPanelRlsBiz.logicDelRls(id, version);
            DmpRegistryRlsUtil.deleteDmpRls(subPanelIdList, ResourceTypeEnum.SUB_PANEL.getValue(), SubPanelVersionConstants.SUB_PANEL_VERSION);

        }
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/


    /**
     * 处理普通板块保存
     *
     * @param inputEntity
     * @param userId
     */
    private void handleSaveNormalPanel(PanelInfoEntity inputEntity, Integer userId) {
        saveBlockRlsInfo(inputEntity.getStatus(), inputEntity.getBlockList(), userId);
        List<BlockContentInfoEntity> totalBlockList = NormalPanelSaveConverter.getFinalBlockList(inputEntity);
        //先保存推荐位信息-这样就会有自增ID了
        blockContentInfoBiz.saveBatchAvoidEmpty(totalBlockList);
        //智能角标合成信息
        List<BlockExtendInfoEntity> totalBlockExtendInfoEntityList = PanelCommonSaveConverter.getFinalBlockExtendInfoEntityList(totalBlockList);
        blockExtendInfoBiz.saveBatchAvoidEmpty(totalBlockExtendInfoEntityList);
        //图片类型，2-替补图，3-背景图,4-异形焦点图,5-高清前景图,6-高清背景图
        blockPictureRlsBiz.handleSaveBlockPictureRlsList(totalBlockList);
        //背景图扩展信息
        blockExtendPosterInfoBiz.handleSaveBlockExtendPoster(totalBlockList);
        //保存推荐位dmp信息
        List<DmpRegistryRlsInfoEntity> totalDmpList = NormalPanelSaveConverter.getFinalDmpList(inputEntity);
        if (CollectionUtils.isNotEmpty(totalDmpList)) {
            dmpRegistryRlsInfoBiz.saveBatchAvoidEmpty(totalDmpList);
        }
        //保存板块和推荐位关联信息
        List<PanelBlockRlsEntity> totalPanelBlockList = NormalPanelSaveConverter.getFinalPanelBlockList(inputEntity);
        panelBlockRlsBiz.saveBatchAvoidEmpty(totalPanelBlockList);
    }

    /**
     * 处理分组板块保存
     *
     * @param inputEntity
     * @param userId
     */
    private void handleSaveGroupPanel(PanelInfoEntity inputEntity, Integer userId) {
        List<SubPanelConfigInfo> subPanelConfigInfoList = inputEntity.getSubPanelList();
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList) {
            saveBlockRlsInfo(inputEntity.getStatus(), subPanelConfigInfo.getBlockList(), userId);
        }
        List<BlockContentInfoEntity> totalBlockList = GroupPanelSaveConverter.getFinalBlockList(inputEntity);
        //先保存推荐位信息-这样就会有自增ID了
        blockContentInfoBiz.saveBatchAvoidEmpty(totalBlockList);
        //智能角标合成信息
        List<BlockExtendInfoEntity> totalBlockExtendInfoEntityList = PanelCommonSaveConverter.getFinalBlockExtendInfoEntityList(totalBlockList);
        blockExtendInfoBiz.saveBatchAvoidEmpty(totalBlockExtendInfoEntityList);
        //图片类型，2-替补图，3-背景图,4-异形焦点图,5-高清前景图,6-高清背景图
        blockPictureRlsBiz.handleSaveBlockPictureRlsList(totalBlockList);
        //背景图扩展信息
        blockExtendPosterInfoBiz.handleSaveBlockExtendPoster(totalBlockList);
        //保存子版块信息
        List<SubPanelInfoEntity> totalSubPanelList = GroupPanelSaveConverter.getFinalSubPanelList(inputEntity);
        subPanelBiz.saveBatchAvoidEmpty(totalSubPanelList);
        //保存推荐位dmp信息
        List<DmpRegistryRlsInfoEntity> totalDmpList = GroupPanelSaveConverter.getFinalDmpList(inputEntity);
        dmpRegistryRlsInfoBiz.saveBatchAvoidEmpty(totalDmpList);
        //保存子板块和推荐位关联信息
        List<PanelBlockRlsEntity> totalPanelBlockList = GroupPanelSaveConverter.getFinalPanelBlockList(inputEntity);
        panelBlockRlsBiz.saveBatchAvoidEmpty(totalPanelBlockList);
        //保存板块和子版块关联信息
        List<PanelSubPanelRlsEntity> totalPanelSubPanelList = GroupPanelSaveConverter.getFinalPanelSubPanelList(inputEntity);
        panelSubPanelRlsBiz.saveBatchAvoidEmpty(totalPanelSubPanelList);
    }

    private void handleSavePluginPanel(PanelInfoEntity inputEntity, Integer userId) {
        if (inputEntity == null) {
            return;
        }
        pluginUpdateProvider.savePanelPluginInfo(inputEntity.getPluginInfo(), inputEntity.getId(), inputEntity.getCurrentVersion());
    }


    /**
     * @param status
     * @param blockList
     * @param userId
     */
    private void saveBlockRlsInfo(Integer status, List<VirtuallyBlockInfo> blockList, Integer userId) {
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            handleEachVirtuallyBlockInfo(status, virtuallyBlockInfo, userId);
        }
    }

    private void handleEachVirtuallyBlockInfo(Integer status, VirtuallyBlockInfo virtuallyBlockInfo, Integer userId) {
        List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
        for (BlockContentConfigInfo blockContentConfigInfo : commonBlockList) {
            handleEachBlockContent(status, blockContentConfigInfo, userId);
        }
        List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
        for (BlockContentConfigInfo dmpBlock : dmpBlockList) {
            handleEachBlockContent(status, dmpBlock, userId);
        }
    }

    private void handleEachBlockContent(Integer status, BlockContentConfigInfo blockContentConfigInfo, Integer userId) {
        blockContentConfigInfo.setGlobalPicture(saveAndGetPoster(blockContentConfigInfo.getPictureUrl(), status, userId));
        //处理角标
        blockContentConfigInfo.setCornerIconIdList(CornerPositionConverter.getCornerIconIdList(blockContentConfigInfo.getCornerList()));
        //推荐位插件
        savePluginInfo(blockContentConfigInfo);
    }

    private GlobalPictureEntity saveAndGetPoster(String pictureUrl, Integer status, Integer userId) {
        GlobalPictureEntity globalPictureEntity;
        if (StringUtils.isBlank(pictureUrl)) {
            globalPictureEntity = new GlobalPictureEntity();
            globalPictureEntity.setId(-1);
        } else {
            globalPictureEntity = globalPictureBiz.queryOrSavePoster(pictureUrl, userId);
        }
        return globalPictureEntity;
    }

    private void savePluginInfo(BlockContentConfigInfo block) {
        if (!BlockTypeConstants.PLUGIN.equals(block.getBlockType())) {
            block.setPluginSelfId(null);
            return;
        }
        block.setPluginSelfId(pluginUpdateProvider.addOrUpdatePluginInfo(block.getPluginInfo()));
    }
}
